package com.step.graphic;

import java.io.InputStream;

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;

import com.step.io.FileHandle;

public class Pixmap {
	public static enum Format {
		ARGB8888, ARGB4444, RGB565
	}
	
	Bitmap bitmap;
	Format format;
	
	public Pixmap(int width, int height, Format format){
		this.bitmap = Bitmap.createBitmap(width, height, getConfig(format));
		this.format = format;
	}
	
	public Pixmap(Bitmap bitmap, Format format){
		this.bitmap = bitmap;
		this.format = format;
	}
	
	public Pixmap(FileHandle file, Format format){
		this.format = format;
		Options options = new Options();
		options.inPreferredConfig = getConfig(format);
		loadBitmap(file, options);		
	}
	
	public Pixmap(FileHandle file){
		loadBitmap(file, null);
		Config config = bitmap.getConfig();
		
		if(config == Config.RGB_565){
			format = Format.RGB565;
		}
		else if(config == Config.ARGB_4444){
			format = Format.ARGB4444;
		}
		else{
			format = Format.ARGB8888;
		}
	}
	
	private void loadBitmap(FileHandle file, Options options){
		InputStream inputStream = null;
		
		try {
			inputStream = file.read();
			if(options != null){
				bitmap = BitmapFactory.decodeStream(inputStream, null, options);
			}
			else{
				bitmap = BitmapFactory.decodeStream(inputStream);
			}
			
			if(bitmap == null){
				throw new RuntimeException("不能从asset文件夹加载位图："+file.path());
			}
		} 
		catch (Exception e) {
			throw new RuntimeException("不能从asset文件夹加载位图："+file.path()+","+e.getMessage());
		}
		finally{
			if(inputStream != null){
				try {
					inputStream.close();
				} 
				catch (Exception e2) {
					e2.printStackTrace();
				}
			}
		}
	}
	
	private Config getConfig(Format format){
		Config config = null;
		
		switch(format){
		case RGB565:
			config = Config.RGB_565;
			break;
		case ARGB4444:
			config = Config.ARGB_4444;
			break;
		case ARGB8888:			
			config = Config.ARGB_8888;
			break;
		default:
			config = Config.ARGB_8888;
			break;
		}
		
		return config;
	}
	
	/**
	 * 改变图像大小
	 * @param dstWidth 目标宽度
	 * @param dstHeight 目标高度
	 * @param scale 是否缩放，值为false,则不按新尺寸缩放图片
	 */
	public Pixmap resize(int dstWidth, int dstHeight, boolean scale){
		Bitmap tempBitmap = Bitmap.createScaledBitmap(bitmap, dstWidth, dstHeight, scale);
		return new Pixmap(tempBitmap, format);
	}
	
	public void getPixels(int[] pixcels, int offset, int stride, int x, int y, int width, int height){
		bitmap.getPixels(pixcels, offset, stride, x, y, width, height);
	}
	
	public void getPixels(int[] pixcels, int x, int y, int width, int height){
		bitmap.getPixels(pixcels, 0, width, x, y, width, height);
	}
	
	public void setPixels(int[] pixcels, int offset, int stride, int x, int y, int width, int height){
		bitmap.setPixels(pixcels, offset, stride, x, y, width, height);
	}
	
	public void setPixels(int[] pixcels, int x, int y, int width, int height){
		bitmap.setPixels(pixcels, 0, width, x, y, width, height);
	}
	
	/** 获取像素宽度 */
	public int getWidth(){
		return bitmap.getWidth();
	}
	
	/** 获取像素高度 */
	public int getHeight(){
		return bitmap.getHeight();
	}
	
	public Format getFormat(){
		return format;
	}
	
	public void dispose(){
		bitmap.recycle();
	}
}
